package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/sirupsen/logrus"
	"github.com/topfreegames/pitaya/v2/client"
)

type TestMessage struct {
	Message string `json:"message"`
}
type TestRespone struct {
	Code int `json:"code"`
}

func main() {

	c := client.New(logrus.InfoLevel, 100*time.Millisecond)
	err := c.ConnectTo("127.0.0.1:8999")
	if err != nil {
		fmt.Println(" conn server error :", err)
		return
	}

	go func(c client.Client) {
		for {
			select {
			case data := <-c.MsgChannel():
				if data.Err {
					fmt.Println("error :", string(data.Data))
					break
				}
				fmt.Println("data --------------", string(data.Data))
			}
		}

	}(*c)

	message, _ := json.Marshal(TestMessage{Message: "xxx"})

	for {

		_, err := c.SendRequest("game.testcomponent.one", []byte("hello"))
		if err != nil {
			fmt.Println("send request error")
			return
		}
		time.Sleep(time.Second)

		_, err = c.SendRequest("game.testcomponent.two", message)
		if err != nil {
			fmt.Println("send request error")
			return
		}
		time.Sleep(time.Second)
	}

}
